home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue3 / Games / xrick / !xrick / src / c / util < prev    next >
Text File  |  2004-06-24  |  5KB  |  210 lines

  1. /*
  2.  * xrick/src/util.c
  3.  *
  4.  * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
  5.  *
  6.  * The use and distribution terms for this software are contained in the file
  7.  * named README, which can be found in the root of this distribution. By
  8.  * using this software in any fashion, you are agreeing to be bound by the
  9.  * terms of this license.
  10.  *
  11.  * You must not remove this notice, or any other, from this software.
  12.  */
  13.  
  14. #include <stdlib.h>  /* NULL */
  15.  
  16. #include "system.h"
  17. #include "config.h"
  18. #include "game.h"
  19. #include "util.h"
  20.  
  21. #include "ents.h"
  22. #include "e_rick.h"
  23. #include "maps.h"
  24.  
  25. /*
  26.  * Full box test.
  27.  *
  28.  * ASM 1199
  29.  *
  30.  * e: entity to test against.
  31.  * x,y: coordinates to test.
  32.  * ret: TRUE/(x,y) is within e's space, FALSE/not.
  33.  */
  34. U8
  35. u_fboxtest(U8 e, S16 x, S16 y)
  36. {
  37.   if (ent_ents[e].x >= x ||
  38.       ent_ents[e].x + ent_ents[e].w < x ||
  39.       ent_ents[e].y >= y ||
  40.       ent_ents[e].y + ent_ents[e].h < y)
  41.     return FALSE;
  42.   else
  43.     return TRUE;
  44. }
  45.  
  46.  
  47.  
  48.  
  49. /*
  50.  * Box test (then whole e2 is checked agains the center of e1).
  51.  *
  52.  * ASM 113E
  53.  *
  54.  * e1: entity to test against (corresponds to DI in asm code).
  55.  * e2: entity to test (corresponds to SI in asm code).
  56.  * ret: TRUE/intersect, FALSE/not.
  57.  */
  58. U8
  59. u_boxtest(U8 e1, U8 e2)
  60. {
  61.   /* rick is special (may be crawling) */
  62.   if (e1 == E_RICK_NO)
  63.     return e_rick_boxtest(e2);
  64.  
  65.   /*
  66.    * entity 1: x+0x05 to x+0x011, y to y+0x14
  67.    * entity 2: x to x+ .w, y to y+ .h
  68.    */
  69.   if (ent_ents[e1].x + 0x11 < ent_ents[e2].x ||
  70.       ent_ents[e1].x + 0x05 > ent_ents[e2].x + ent_ents[e2].w ||
  71.       ent_ents[e1].y + 0x14 < ent_ents[e2].y ||
  72.       ent_ents[e1].y > ent_ents[e2].y + ent_ents[e2].h - 1)
  73.     return FALSE;
  74.   else
  75.     return TRUE;
  76. }
  77.  
  78.  
  79. /*
  80.  * Compute the environment flag.
  81.  *
  82.  * ASM 0FBC if !crawl, else 103E
  83.  *
  84.  * x, y: coordinates where to compute the environment flag
  85.  * crawl: is rick crawling?
  86.  * rc0: anything CHANGED to the environment flag for crawling (6DBA)
  87.  * rc1: anything CHANGED to the environment flag (6DAD)
  88.  */
  89. void
  90. u_envtest(S16 x, S16 y, U8 crawl, U8 *rc0, U8 *rc1)
  91. {
  92.   U8 i, xx;
  93.  
  94.   /* prepare for ent #0 test */
  95.   ent_ents[ENT_ENTSNUM].x = x;
  96.   ent_ents[ENT_ENTSNUM].y = y;
  97.  
  98.   i = 1;
  99.   if (!crawl) i++;
  100.   if (y & 0x0004) i++;
  101.  
  102.   x += 4;
  103.   xx = (U8)x; /* FIXME? */
  104.  
  105.   x = x >> 3;  /* from pixels to tiles */
  106.   y = y >> 3;  /* from pixels to tiles */
  107.  
  108.   *rc0 = *rc1 = 0;
  109.  
  110.   if (xx & 0x07) {  /* tiles columns alignment */
  111.     if (crawl) {
  112.       *rc0 |= (map_eflg[map_map[y][x]] &
  113.        (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  114.       *rc0 |= (map_eflg[map_map[y][x + 1]] &
  115.        (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  116.       *rc0 |= (map_eflg[map_map[y][x + 2]] &
  117.        (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  118.       y++;
  119.     }
  120.     do {
  121.       *rc1 |= (map_eflg[map_map[y][x]] &
  122.            (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|
  123.         MAP_EFLG_LETHAL|MAP_EFLG_01));
  124.       *rc1 |= (map_eflg[map_map[y][x + 1]] &
  125.            (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|
  126.         MAP_EFLG_LETHAL|MAP_EFLG_CLIMB|MAP_EFLG_01));
  127.       *rc1 |= (map_eflg[map_map[y][x + 2]] &
  128.            (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|
  129.         MAP_EFLG_LETHAL|MAP_EFLG_01));
  130.       y++;
  131.     } while (--i > 0);
  132.  
  133.     *rc1 |= (map_eflg[map_map[y][x]] &
  134.          (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP|MAP_EFLG_FGND|
  135.           MAP_EFLG_LETHAL|MAP_EFLG_01));
  136.     *rc1 |= (map_eflg[map_map[y][x + 1]]);
  137.     *rc1 |= (map_eflg[map_map[y][x + 2]] &
  138.          (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP|MAP_EFLG_FGND|
  139.           MAP_EFLG_LETHAL|MAP_EFLG_01));
  140.   }
  141.   else {
  142.     if (crawl) {
  143.       *rc0 |= (map_eflg[map_map[y][x]] &
  144.        (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  145.       *rc0 |= (map_eflg[map_map[y][x + 1]] &
  146.        (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  147.       y++;
  148.     }
  149.     do {
  150.       *rc1 |= (map_eflg[map_map[y][x]] &
  151.            (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|
  152.         MAP_EFLG_LETHAL|MAP_EFLG_CLIMB|MAP_EFLG_01));
  153.       *rc1 |= (map_eflg[map_map[y][x + 1]] &
  154.            (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|
  155.         MAP_EFLG_LETHAL|MAP_EFLG_CLIMB|MAP_EFLG_01));
  156.       y++;
  157.     } while (--i > 0);
  158.  
  159.     *rc1 |= (map_eflg[map_map[y][x]]);
  160.     *rc1 |= (map_eflg[map_map[y][x + 1]]);
  161.   }
  162.  
  163.   /*
  164.    * If not lethal yet, and there's an entity on slot zero, and (x,y)
  165.    * boxtests this entity, then raise SOLID flag. This is how we make
  166.    * sure that no entity can move over the entity that is on slot zero.
  167.    *
  168.    * Beware! When game_cheat2 is set, this means that a block can
  169.    * move over rick without killing him -- but then rick is trapped
  170.    * because the block is solid.
  171.    */
  172.   if (!(*rc1 & MAP_EFLG_LETHAL)
  173.       && ent_ents[0].n
  174.       && u_boxtest(ENT_ENTSNUM, 0)) {
  175.     *rc1 |= MAP_EFLG_SOLID;
  176.   }
  177.  
  178.   /* When game_cheat2 is set, the environment can not be lethal. */
  179. #ifdef ENABLE_CHEATS
  180.   if (game_cheat2) *rc1 &= ~MAP_EFLG_LETHAL;
  181. #endif
  182. }
  183.  
  184.  
  185. /*
  186.  * Check if x,y is within e trigger box.
  187.  *
  188.  * ASM 126F
  189.  * return: FALSE if not in box, TRUE if in box.
  190.  */
  191. U8
  192. u_trigbox(U8 e, S16 x, S16 y)
  193. {
  194.   U16 xmax, ymax;
  195.  
  196.   xmax = ent_ents[e].trig_x + (ent_entdata[ent_ents[e].n & 0x7F].trig_w << 3);
  197.   ymax = ent_ents[e].trig_y + (ent_entdata[ent_ents[e].n & 0x7F].trig_h << 3);
  198.  
  199.   if (xmax > 0xFF) xmax = 0xFF;
  200.  
  201.   if (x <= ent_ents[e].trig_x || x > xmax ||
  202.       y <= ent_ents[e].trig_y || y > ymax)
  203.     return FALSE;
  204.   else
  205.     return TRUE;
  206. }
  207.  
  208.  
  209. /* eof */
  210.